package ils.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import ils.domain.ugc.model.Review;
import ils.services.UgcService;
import ils.util.SpringUtil;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class CreateReviewAction extends ActionSupport implements SessionAware, Preparable
{
    private static final Logger logger = LoggerFactory.getLogger(CreateReviewAction.class);
    private UgcService ugcService;
    private String msg;
    private int bookId;
    private String review;
    private int rating;
    private Map<String, Object> session;
    public String execute() throws Exception
    {
        logger.debug("entering create review");
        Review reviewObject = new Review();
        reviewObject.setBookId(bookId);
        reviewObject.setRating(rating);
        reviewObject.setReview(review);
        int userId = (Integer)session.get("userId");
        reviewObject.setUserId(userId);
        ugcService.createReview(reviewObject);
        msg = "successfullly created review";
        logger.debug("created review");
        return SUCCESS;
    }
    public void setUgcService(UgcService ugcService)
    {
        this.ugcService = ugcService;
    }
    public String getMsg()
    {
        return msg;
    }
    @Override
    public void setSession(Map<String, Object> arg0)
    {
        this.session = arg0;
    }
    public void setBookId(int bookId)
    {
        this.bookId = bookId;
    }
    public void setReview(String review)
    {
        this.review = review;
    }
    public void setRating(int rating)
    {
        this.rating = rating;
    }

    @Override
    public void prepare() throws Exception {
        ugcService = (UgcService)SpringUtil.getApplicationContext().getBean("ugcService");
    }
}
